﻿using AnlianCommon.API.Commands.Flats;
using AnlianCommon.Common.Domains.BeginningManagement;
using AnlianCommon.Common.Domains.OrganizationalUnits;
using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AnlianCommon.API.Commands.Startofseasons
{
    public class StartofseasonCreateHander : IRequestHandler<StartofseasonCreateCommand, Result>
    {
        private readonly IBaseRepository<Startofseason> _startofseasonRepository;
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;
        private readonly IBaseRepository<StatusLevels> _statusLevelsRepository;
        private readonly IMapper _mapper;

        public StartofseasonCreateHander(IBaseRepository<Startofseason> startofseasonRepository, IBaseRepository<SheepArchives> sheepArchivesRepository, IBaseRepository<StatusLevels> statusLevelsRepository, IMapper mapper)
        {
            _startofseasonRepository = startofseasonRepository;
            _sheepArchivesRepository = sheepArchivesRepository;
            _statusLevelsRepository = statusLevelsRepository;
            _mapper = mapper;
        }

        public async Task<Result> Handle(StartofseasonCreateCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Added };
            try
            {
                Startofseason  startofseason = _mapper.Map<Startofseason>(request);
                startofseason.CreatedOwnerId = "张三";
                startofseason.CreatedTime = DateTime.Now;
                startofseason.Elements = 1;
                var res = await _startofseasonRepository.Add(startofseason);
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Msg = "添加成功";
                    //期初新增完 查询档案表条件耳号
                    var sheepArchives = await (await _sheepArchivesRepository.GetAll()).Where(c=>c.RecordAuricle==startofseason.Auricle).FirstOrDefaultAsync();
                    //如果不为空进行修改
                    if (sheepArchives != null)
                    {
                        sheepArchives.CottageId = startofseason.CottageId;//档案栋舍赋值==期初新增的栋舍
                        sheepArchives.FieldId = startofseason.Field;//档案栏位赋值=期初新增的栏位
                        sheepArchives.PresenceStatus = 1;//1在场,2不在场 档案在场状态=在场
                        var results = await _sheepArchivesRepository.Update(sheepArchives);
                    }
                    
                }
                else
                {
                    result.Code = (int)CommonErrorCode.Added;
                    result.Msg = "添加失败";
                }

            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
